package com.xmutca.core.websocket.handler;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.stream.ChunkedWriteHandler;

public class ChildChannelHandler extends ChannelInitializer<SocketChannel> {

	private FullHttpRequestHandler fullHttpRequestHandler;
	private TextWebSocketFrameHandler textWebSocketFrameHandler;
	private CloseWebSocketFrameHandler closeWebSocketFrameHandler;

	@Override
	protected void initChannel(SocketChannel e) throws Exception {
		e.pipeline().addLast("http-codec", new HttpServerCodec());
		e.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
		e.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
		e.pipeline().addLast(fullHttpRequestHandler);
		e.pipeline().addLast(textWebSocketFrameHandler);
		e.pipeline().addLast(closeWebSocketFrameHandler);
	}

	public void setFullHttpRequestHandler(FullHttpRequestHandler fullHttpRequestHandler) {
		this.fullHttpRequestHandler = fullHttpRequestHandler;
	}

	public void setTextWebSocketFrameHandler(TextWebSocketFrameHandler textWebSocketFrameHandler) {
		this.textWebSocketFrameHandler = textWebSocketFrameHandler;
	}

	public void setCloseWebSocketFrameHandler(CloseWebSocketFrameHandler closeWebSocketFrameHandler) {
		this.closeWebSocketFrameHandler = closeWebSocketFrameHandler;
	}
}
